import axios from 'axios'
import { ElMessage } from 'element-plus'
import store from "../store/index.js";
import JSONbig  from 'json-bigint';

const JsonBigToString = JSONbig({ storeAsString: true })

const request = axios.create({
  timeout: 100000,
  headers: {
    'Content-Type': 'application/json; charset=utf-8'
  },
  transformResponse: [function (data) {
    try {
      //转换
      return JsonBigToString.parse(data);
    } catch (err) {
      //转换失败就直接按原数据返回
      return data;

    }
  }]
})

request.defaults.baseURL = store.getters.getRequestUrl

// 前置拦截
request.interceptors.request.use(config => {
   //config.headers.Authorization = 'You are welcome to steal'
  config.headers['Authorization'] = localStorage.getItem("token");
  //config.withCredentials = true
  return config
})
// 后置拦截
request.interceptors.response.use(response => {
  const res = response.data
  if (res.code === 200) {
    return response
  } else {
     //ElMessage.error('小主人,有bug哦')
    //ElMessage.warning(response.data.msg)
    return Promise.reject(response.data.msg)
  }
},
error => {
  // console.log(error)
  if (error.response.status === 500) {
    //ElMessage.error('错误访问')
  }
  return Promise.reject(error)
}
)
export default request
